package com.lyfei.yd.web;

import com.lyfei.yd.upload.OssUtils;
import com.lyfei.yd.upload.QiNiuUtils;
import com.lyfei.yd.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private QiNiuUtils qiNiuUtils;

    @PostMapping("/upload")
    public AjaxResult uploadFileQiNiu(MultipartFile file) {
        try {
            List<String> result = qiNiuUtils.upload(file);
            if(result.size()==2){
                AjaxResult ajax = AjaxResult.success();
                ajax.put("url", result.get(1));
                ajax.put("fileName", result.get(0));
                return ajax;
            }else{
                return AjaxResult.error("图片上传失败");
            }
        }catch (Exception ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }

    @PostMapping("/uploadOss")
    public AjaxResult uploadFileOss(MultipartFile file) {
        try {
            List<String> result = OssUtils.upload(file);
            if(result.size()==2){
                AjaxResult ajax = AjaxResult.success();
                ajax.put("url", result.get(1));
                ajax.put("fileName", result.get(0));
                return ajax;
            }else{
                return AjaxResult.error("图片上传失败");
            }
        }catch (Exception ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }
}
